<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选动态类型 -->
			<cl-filter label="动态类型">
				<cl-select prop="type" :options="options.type" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索主播名称、动态内容" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="anchorDynamic-dynamic" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { reactive, onMounted, ref } from "vue";

const { service } = useCool();

// 选项
const options = reactive({
	type: [
		{ label: "普通", value: 0, type: "danger" },
		{ label: "热门", value: 1, type: "success" }
	],
	tollType: [
		{ label: "免费", value: 0, type: "danger" },
		{ label: "收费", value: 1, type: "success" }
	]
});
let anchorOptions = ref([]);

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "主播",
			prop: "anchorId",
			component: {
				name: "el-select",
				options: anchorOptions,
				props: { clearable: true, filterable:true }
			},
			col: {
				span: 12
			},
			required: true
		},
		{
			label: "动态内容",
			prop: "content",
			component: { name: "el-input", props: { type: "textarea", rows: 4 } },
			required: true
		},
		{
			label: "发布时间",
			prop: "publishTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" }
			},
			required: true
		},
		{
			label: "金币",
			prop: "goldcoin",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "点赞数",
			prop: "likes",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "评论数",
			prop: "comments",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "观看数",
			prop: "views",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "动态类型",
			prop: "type",
			component: { name: "el-radio-group", options: options.type },
			value: 0,
			required: true
		},
		{
			label: "动态权限",
			prop: "isToll",
			component: { name: "el-radio-group", options: options.tollType },
			value: 0,
			required: true
		},
		{ label: "封面图", prop: "coverImage", component: { name: "cl-upload" } },
		{
			label: "动态图",
			prop: "dynamicImages",
			component: { name: "cl-upload", props: { multiple: true } }
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "主播名称", prop: "anchorName", minWidth: 140 },
		{ label: "动态内容", prop: "content", showOverflowTooltip: true, minWidth: 200 },
		{
			label: "发布时间",
			prop: "publishTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{ label: "点赞数", prop: "likes", minWidth: 140 },
		{ label: "评论数", prop: "comments", minWidth: 140 },
		{ label: "观看数", prop: "views", minWidth: 140 },
		{ label: "动态类型", prop: "type", dict: options.type, minWidth: 120 },
		{
			label: "封面图",
			prop: "coverImage",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{
			label: "动态图",
			prop: "dynamicImages",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.anchorDynamic.dynamic
	},
	(app) => {
		app.refresh();
	}
);

const GetAnchorOptions = async () => {
	let data = await service.demo.info.list();
	anchorOptions.value = data.map((o: any) => {
		return {
			value: o.id + "",
			label: o.name
		};
	});
};

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
onMounted(() => {
	GetAnchorOptions();
});
</script>
